No. Variables (such as y
)
cannot be put in an interface.
Only constants and method declarations.
interface SomeInterface
{
public final int x = 32;
public double y; // No variables allowed
public double addup( );
}
A class definition must always extend one parent, but it can implement zero or more interfaces:
class SomeClass extends Parent implements SomeInterface { class definition body }
The body of the class definition is the same as always. However, since it implements an interface the body must have a definition of each of the methods declared in the interface. The class definition can use access modifiers as usual. Here is a class definition that implements three interfaces:
public class BigClass extends Parent implements InterfaceA, InterfaceB, InterfaceC { class definition body }
Now BigClass
must provide a method definition for every method
declared in the three interfaces.
Here is another class definition:
public class SmallClass implements InterfaceA { class definition body }
Any number of classes can implement the same interfaces.
Is the above class definition correct? What parent class does it extend?